package com.hbisedm.interninfosys.intern.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbisedm.interninfosys.commonutils.R;
import com.hbisedm.interninfosys.intern.entity.StuApply;
import com.hbisedm.interninfosys.intern.entity.StuInternship;
import com.hbisedm.interninfosys.intern.entity.User;
import com.hbisedm.interninfosys.intern.service.IStuApplyService;
import com.hbisedm.interninfosys.intern.service.IStuInternshipService;
import com.hbisedm.interninfosys.intern.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2021-02-01
 */
@RestController
@RequestMapping("/intern/stu-apply")
public class StuApplyController {
    @Autowired
    IStuApplyService stuApplyService;

    @Autowired
    IStuInternshipService stuInternshipService;

    @Autowired
    IUserService userService;

    /**
     * 学生申请企业接口
     * @param stuApply
     * @return
     */
    @PostMapping("addApply")
    public R addApply(@RequestBody StuApply stuApply) {
        stuApply.setApplyStatus(0);
        boolean save = stuApplyService.save(stuApply);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 获取申请状态
     * @param stuApply
     * @return
     */
    @PostMapping("getApplyStatus")
    public R getApplyStatus(@RequestBody StuApply stuApply) {
        if(stuApply.getIid() != null && stuApply.getSid() != null) {
            StuApply one = stuApplyService.getOne(new QueryWrapper<StuApply>().lambda().eq(StuApply::getIid, stuApply.getIid()).eq(StuApply::getSid, stuApply.getSid()));
            if(one != null) {
                Integer applyStatus = one.getApplyStatus();
                if(applyStatus.equals(1)) {
                    return R.ok().data("status", "已加入,无需申请").data("enable", 0);
                }else {
                    return R.ok().data("status", "已申请").data("enable", 2);
                }
            }
            return R.ok().data("status", "申请").data("enable", 1);
        }
        return R.ok().data("status", "申请").data("enable", 1);
    }

    /**
     * 获取学生申请列表
     * @param stuApply
     * @return
     */
    @PostMapping("getApplyStuList")
    public R getApplyStuList(@RequestBody StuApply stuApply) {
        Integer iid = stuApply.getIid();
        ArrayList<User> users = new ArrayList<>();
        List<StuApply> list = stuApplyService.list(new QueryWrapper<StuApply>().lambda().eq(StuApply::getIid, iid).eq(StuApply::getApplyStatus, 0));
        list.stream().forEach(item -> {
            User user = userService.getById(item.getSid());
            users.add(user);
        });
        return R.ok().data("stuList", users);
    }

    /**
     * 审核通过
     */
    @PostMapping("/pass")
    public R pass(@RequestBody StuApply stuApply) {
        /**
         * 新建个stuIntern
         * 删除stuApply
         */
        StuInternship internship = new StuInternship();
        internship.setIid(stuApply.getIid());
        internship.setSid(stuApply.getSid());
        StuApply one = stuApplyService.getOne(new QueryWrapper<StuApply>().lambda().eq(StuApply::getIid, stuApply.getIid()).eq(StuApply::getSid, stuApply.getSid()));

        stuApplyService.removeById(one);
        stuInternshipService.save(internship);

        return R.ok();
    }

}
